package com.gosophia.reportData.action;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.flowForm.entity.FlowForm;
import com.gosophia.flowForm.service.FlowFormManageService;
import com.gosophia.flowFormManagement.dto.FormInstanceDTO;
import com.gosophia.flowFormManagement.ws.FormInstanceWebService;
import com.gosophia.reportData.dto.ApprovalReportJobDTO;
import com.gosophia.reportData.dto.LaunchReportJobDTO;
import com.gosophia.reportData.dto.ReportJobDTO;
import com.gosophia.reportData.ws.LaunchReportWebService;
import com.gosophia.reportData.ws.ReportDataSearchWebService;
import com.gosophia.reportData.ws.ReportJobWebService;

/**
 * 查看历史记录详情
 * @author guodongpo
 *
 */
@Controller("reportDataDetailAction")
@Scope("prototype")
public class ReportDataDetailAction extends BaseSupportAction {

    private static final long serialVersionUID = 1L;

    private String jobType;

    private String jobState;

    @Autowired
    private ReportDataSearchWebService reportDataSearchWebService;
    @Autowired
    private FlowFormManageService flowFormManageService;
    @Autowired
    private LaunchReportWebService launchReportWebService;
    @Autowired
	private FormInstanceWebService formInstanceWebService;
	@Autowired
	private ReportJobWebService reportJobWebService;
    private FlowForm flowForm;
    private ReportJobDTO reportJobDto;

    private ApprovalReportJobDTO approvalDto;
    private LaunchReportJobDTO launchReportJobDTO;
    private Map<String, Object> newDataInstance;

    private Long jobId;

    public String _execute() throws Exception {
    	
        String jobtype = getByteType(jobType);
        String jobstate = getByteType(jobState);
        
        //取得发起节点
        launchReportJobDTO = launchReportWebService.findFirstJobByJobId(jobId);
        if (jobtype.equals("上报")) {
            reportJobDto = reportDataSearchWebService.findReportDetail(jobId);
            if (launchReportJobDTO.getReportMethod().equals("F")) {
                return "attReport";//7 附件上报
            } else {
                if (launchReportJobDTO.getFlowFormDto().getModel().equals('E') && launchReportJobDTO.getFlowFormDto().getModel() == 'E') {
                    flowForm = flowFormManageService
                            .findFlowFormById(launchReportJobDTO
                                    .getFlowFormDto().getFormId());
                    return "formDataReport";// excel上报
                } else if (launchReportJobDTO.getFlowFormDto().getModel().equals('F') && launchReportJobDTO.getFlowFormDto().getModel() == 'F') {
                	FormInstanceDTO formInstanceDTO = formInstanceWebService
					.findFormInstanceById(reportJobDto.getFormInstanceId());
                	Map<Long, Object> dataInstance = formInstanceDTO
					.getInstanceData();
                	newDataInstance = reportJobWebService
					.findFlowFormData(dataInstance);
                	return "flowFormReport";// 电子表单上报
                } else {
                	throw new RuntimeException();
                }
            }
        } else if (jobtype.equals("审核")) {
            if (jobstate.equals("已通过")) {
                approvalDto = reportDataSearchWebService
                        .findPassReportDetail(jobId);
            } else{
            	approvalDto = reportDataSearchWebService.findRejectReportDetail(jobId);
            }
//            else if (jobstate.equals("已退回")) {
//                approvalDto = reportDataSearchWebService
//                        .findRejectReportDetail(jobId);
//            }
            return "approve";
        } else if (jobtype.equals("发起")) {
            launchReportJobDTO = launchReportWebService
                    .findFirstJobByJobId(jobId);
            return "launch";
        } else{
        	return SUCCESS;
        }
    }
    
	public String getByteType(String str){
		try {
			if (str.equals(new String(str.getBytes("GBK"), "GBK"))) {
				str = str.trim();
			} else {
				str = new String(str.getBytes("ISO8859_1"), "UTF-8")
						.trim();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

    public String getJobType() {
        return jobType;
    }

    public void setJobType(String jobType) {
        this.jobType = jobType;
    }

    public String getJobState() {
        return jobState;
    }

    public void setJobState(String jobState) {
        this.jobState = jobState;
    }

    public ReportJobDTO getReportJobDto() {
        return reportJobDto;
    }

    public void setReportJobDto(ReportJobDTO reportJobDto) {
        this.reportJobDto = reportJobDto;
    }

    public ApprovalReportJobDTO getApprovalDto() {
        return approvalDto;
    }

    public void setApprovalDto(ApprovalReportJobDTO approvalDto) {
        this.approvalDto = approvalDto;
    }

    public Long getJobId() {
        return jobId;
    }

    public void setJobId(Long jobId) {
        this.jobId = jobId;
    }

    public LaunchReportJobDTO getLaunchReportJobDTO() {
        return launchReportJobDTO;
    }

    public void setLaunchReportJobDTO(LaunchReportJobDTO launchReportJobDTO) {
        this.launchReportJobDTO = launchReportJobDTO;
    }

    public FlowForm getFlowForm() {
        return flowForm;
    }

    public void setFlowForm(FlowForm flowForm) {
        this.flowForm = flowForm;
    }

	public Map<String, Object> getNewDataInstance() {
		return newDataInstance;
	}

	public void setNewDataInstance(Map<String, Object> newDataInstance) {
		this.newDataInstance = newDataInstance;
	}
}
